package com.nbjtjc.safe.service.emg;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgDutyMapper;
import com.nbjtjc.safe.model.emg.EmgDuty;
import com.nbjtjc.safe.service.sys.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmgDutyService {

    @Autowired
    private EmgDutyMapper emgDutyMapper;

    @Autowired
    private SysDeptService sysDeptService;
    public Result find(EmgDuty emgDuty, Page page){
        Result result=new Result();
        PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
        try {
            List<EmgDuty> emgDuties=emgDutyMapper.search(emgDuty);
            result.setData(new PageInfo<>(emgDuties));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    public Result delete(Integer emgDutyId){
        Result result=new Result();
        if(ObjectUtil.isNull(emgDutyId)){
            result.setFalse("删除对象不能为空");
            return result;
        }
        emgDutyMapper.deleteByPrimaryKey(emgDutyId);
        return result;
    }

    public Result add(EmgDuty emgDuty){
        Result result=new Result();
        if(ObjectUtil.isNull(emgDuty)
                ||ObjectUtil.isNull(emgDuty.getDutyDate())
                ||ObjectUtil.isNull(emgDuty.getPlanPublicId())
                ||ObjectUtil.isNull(emgDuty.getCompanyId())){
            result.setFalse("关键字段不能为空");
            return result;
        }
        emgDuty.setCompany(sysDeptService.getCompanyName());
        int res=emgDutyMapper.insertSelective(emgDuty);
        if(res==0){
            result.setFalse("插入失败");
        }
        result.setData(emgDuty);
        return result;
    }

}
